package com.lzx.guanchajava.view.fragment.ItemListFragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.lzx.guanchajava.pojo.TAG
import com.lzx.guanchajava.pojo.bean.newsList.News
import com.lzx.guanchajava.view.activity.NewsActivity
import com.lzx.guanchajava.view.activity.PostActivity
import com.lzx.guanchajava.view.activity.TagsActivity
import com.lzx.guanchajava.adapter.newsListAdapter.*
import com.lzx.guanchajava.view.fragment.base.BaseListFragment
import com.lzx.guanchajava.util.ImageUtils
import com.lzx.guanchajava.util.JumpUtil
import com.lzx.guanchajava.viewmodel.news.vmNewsList

/**
 * 暂时没用到
 */
class NewsListFragment : BaseListFragment<NewsListAdapter>()  {

    private val mNewsListViewModel by lazy {
        ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(vmNewsList::class.java)
    }


    companion object {
        fun newInstance(url: String): NewsListFragment = NewsListFragment().apply {
            arguments = Bundle().apply {
                putString(TAG.TAG_FRAGMENT_NEWS_LIST_URL, url)
            }
        }

        fun newInstance(url: String, loadNow: Boolean): NewsListFragment = NewsListFragment().apply {
            arguments = Bundle().apply {
                putString(TAG.TAG_FRAGMENT_NEWS_LIST_URL, url)
                putBoolean(TAG.TAG_FRAGMENT_NEWS_LIST_LOADNOW, loadNow)
            }
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mNewsListViewModel.newsList.observe(viewLifecycleOwner, Observer {
            handleData(it)
        })
    }

    private fun handleData(data: List<News>) {
        if (mPageNo == 1) {
            mAdapter.data = data as MutableList<News>
            mAdapter.notifyDataSetChanged()
            mSwipe.isRefreshing = false
            mPageNo = 2
        } else {
            val p1 = mAdapter.itemCount
            if (data.isEmpty()) hasMore = false
            mAdapter.data.addAll(data)
            val p2 = mAdapter.itemCount
            mAdapter.notifyItemRangeInserted(p1, p2)
        }
    }


    override fun getAdapter(): NewsListAdapter = NewsListAdapter().apply {
        types = listOf(Type1(), Type2(), Type3(), Type4(), Type5(), Type9())
        onClickListener = object : NewsListAdapter.OnClickListener {
            override fun onClick(type: Int, id: String) {
                when (type) {
                    1 -> {
                        goto(NewsActivity::class.java, "ID" to id, "CODETYPE" to "1")
                    }     //新闻
                    2 -> {
                        goto(PostActivity::class.java,"ID" to id, "CODETYPE" to "2")
                    }     //风闻
                    3 -> toTagsAct(id)      //标签
                    else -> {

                    }
                }
            }
        }
    }

    private val onTagClickObj = object : INewsType.OnClickListener {
        override fun onTagClick(id: String) {
            toTagsAct(id)
        }

        override fun onLongClick(imageUrl: String, ancherView: View) {
            ImageUtils.saveImageWithPopup(imageUrl, ancherView)
        }

        override fun onClick(id: String, type: String) {
            if (type == "1") JumpUtil.startActivity(requireActivity(), NewsActivity::class.java, "ID" to id, "CODETYPE" to type)
            else JumpUtil.startActivity(requireActivity(), PostActivity::class.java, "ID" to id, "CODETYPE" to type)
        }
    }

    private fun toTagsAct(id: String) {
        goto(TagsActivity::class.java, TAG.ACTIVITY_TAGS to id)
    }


    override fun requestData(adapter: NewsListAdapter, swipe: SwipeRefreshLayout) {
        mNewsListViewModel.getNewsList(mApi + 1)
    }

    override fun requestMore(mAdapter: NewsListAdapter) {
        mNewsListViewModel.getNewsList(mApi + mPageNo++)
    }
}